package com.echo.register.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.echo.register.common.exception.RegisterException;
import com.echo.register.common.result.ResultCodeEnum;
import com.echo.register.hosp.mapper.HospitalSetMapper;
import com.echo.register.hosp.service.HospitalSetService;
import com.echo.register.model.hosp.HospitalSet;
import com.echo.register.vo.order.SignInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @ClassName: HospitalSetServiceImpl
 * @Package: com.echo.register.hosp.service.impl
 * @Description: 医院设置管理
 * @Author 谢同学
 * @Create 2023/12/10 16:36
 * @Version 1.0
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    /**
     * 根据医院编号获取签名key
     *
     * @param hoscode 医院编号
     * @return 签名key
     */
    @Override
    public String getSingKey(String hoscode) {
        // 获取医院编号
        HospitalSet hospitalSet = baseMapper.selectOne(new LambdaQueryWrapper<HospitalSet>()
                .eq(HospitalSet::getHoscode, hoscode));
        // 判断医院是否存在
        if (StringUtils.isEmpty(hospitalSet)) {
            throw new RegisterException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        if (hospitalSet.getStatus() == 0) {
            throw new RegisterException(ResultCodeEnum.HOSPITAL_LOCK);
        }
        return hospitalSet.getSignKey();
    }

    /**
     * 获取签名信息
     *
     * @param hoscode 医院编号
     * @return
     */
    @Override
    public SignInfoVo getSingKeyOrder(String hoscode) {
        HospitalSet hospitalSet = baseMapper.selectOne(new LambdaQueryWrapper<HospitalSet>().eq(HospitalSet::getHoscode, hoscode));
        if (hospitalSet != null) {
            SignInfoVo signInfoVo = new SignInfoVo();
            signInfoVo.setSignKey(hospitalSet.getSignKey());
            signInfoVo.setApiUrl(hospitalSet.getApiUrl());
            return signInfoVo;
        }
        return null;
    }

}
